home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_11 / 9n11076a < prev    next >
Text File  |  1991-04-10  |  13KB  |  498 lines

  1.  
  2.  
  3.        /************************************************
  4.        *
  5.        *       file d:\lsu\wtiff.c
  6.        *
  7.        *       Functions: This file contains
  8.        *           create_allocate_tiff_file
  9.        *           write_array_into_tiff_image
  10.        *           write_line
  11.        *           insert_short_into_buffer
  12.        *           insert_long_into_buffer
  13.        *           round_off_image_size
  14.        *           does_not_exist
  15.        *
  16.        *       Purpose:
  17.        *          These functions insert a 100x100 array into
  18.        *          a tiff image already stored on disk.
  19.        *
  20.        *       External Calls:
  21.        *          rtiff.c - seek_to_first_line
  22.        *                    seek_to_end_of_line
  23.        *          tiff.c - read_tiff_header
  24.        *          mrw.c - my_write
  25.        *
  26.        *       Modifications:
  27.        *          29 January 1991 - created
  28.        *
  29.        *************************************************/
  30.  
  31.  
  32.  
  33. #include "d:\cips\cips.h"
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.        /*******************************************************
  43.        *
  44.        *   create_alllocate_tiff_file(...
  45.        *
  46.        *   This function creates a file on disk that will be
  47.        *   large enough to hold a tiff image.  The input
  48.        *   tiff_header_struct describes the desired tiff file.
  49.        *   This function writes the tiff header and then
  50.        *   writes a blank image array out to disk the proper
  51.        *   number of times.  This has the effect of allocating
  52.        *   the correct number of bytes on the disk.
  53.        *
  54.        *******************************************************/
  55.  
  56.  
  57. create_allocate_tiff_file(file_name, image_header, image)
  58.    char   file_name[];
  59.    short  image[ROWS][COLS];
  60.    struct tiff_header_struct *image_header;
  61. {
  62.    char  buffer[12];
  63.  
  64.    int   bytes_written,
  65.          file_desc,
  66.          i,
  67.          j,
  68.          l,
  69.          w;
  70.  
  71.    long  k;
  72.  
  73.  
  74.  
  75.       /***************************************
  76.       *
  77.       *   Create the image file in binary mode
  78.       *   for both reading and writing.
  79.       *
  80.       ****************************************/
  81.  
  82.    file_desc = open(file_name,
  83.           O_CREAT | O_RDWR | O_BINARY,
  84.           S_IREAD | S_IWRITE);
  85.    printf("\n file desc=%d", file_desc);
  86.  
  87.       /***************************************
  88.       *
  89.       *   Write out the first 8 bytes of the
  90.       *   header.  The meaning of the
  91.       *   bytes (HEX) is:
  92.       *      0-1 = 49 49 - LSB first
  93.       *      2-3 = 2A 00 - version #
  94.       *      4-7 = 08 00 00 00 - go to offset
  95.       *           8 for the first
  96.       *           Image File
  97.       *           Directory
  98.       *
  99.       ****************************************/
  100.  
  101.    buffer[0] = 0x49;
  102.    buffer[1] = 0x49;
  103.    buffer[2] = 0x2A;
  104.    buffer[3] = 0x00;
  105.    buffer[4] = 0x08;
  106.    buffer[5] = 0x00;
  107.    buffer[6] = 0x00;
  108.    buffer[7] = 0x00;
  109.  
  110.    bytes_written = my_write(file_desc, buffer, 8);
  111.  
  112.    printf("\n wrote %d bytes", bytes_written);
  113.  
  114.       /***************************************
  115.       *
  116.       *   Write out the first 2 bytes of the
  117.       *   Image File Directory.  These tell
  118.       *   the number of entries in the IFD.
  119.       *
  120.       ****************************************/
  121.  
  122.    buffer[0] = 0x05;
  123.    buffer[1] = 0x00;
  124.    bytes_written = my_write(file_desc, buffer, 2);
  125.  
  126.    printf("\n wrote %d bytes", bytes_written);
  127.  
  128.       /***************************************
  129.       *
  130.       *   Write out the entries into the
  131.       *   Image File Directory.
  132.       *
  133.       ****************************************/
  134.  
  135.       /* Subfile Type */
  136.    buffer[0]  = 0xFF;
  137.    buffer[1]  = 0x00;
  138.    buffer[2]  = 0x03;
  139.    buffer[3]  = 0x00;
  140.    buffer[4]  = 0x01;
  141.    buffer[5]  = 0x00;
  142.    buffer[6]  = 0x00;
  143.    buffer[7]  = 0x00;
  144.    buffer[8]  = 0x01;
  145.    buffer[9]  = 0x00;
  146.    buffer[10] = 0x00;
  147.    buffer[11] = 0x00;
  148.  
  149.    bytes_written = my_write(file_desc, buffer, 12);
  150.  
  151.    printf("\n wrote %d bytes", bytes_written);
  152.  
  153.       /* Image Width */
  154.    insert_short_into_buffer(buffer, 0, 256);
  155.    insert_short_into_buffer(buffer, 2, 3);
  156.    insert_short_into_buffer(buffer, 4, 1);
  157.    insert_short_into_buffer(buffer, 8, image_header->image_width);
  158.  
  159.    bytes_written = my_write(file_desc, buffer, 12);
  160.    printf("\n wrote %d bytes", bytes_written);
  161.  
  162.       /* Image Length */
  163.    insert_short_into_buffer(buffer, 0, 257);
  164.    insert_short_into_buffer(buffer, 2, 3);
  165.    insert_short_into_buffer(buffer, 4, 1);
  166.    insert_short_into_buffer(buffer, 8, image_header->image_length);
  167.  
  168.    bytes_written = my_write(file_desc, buffer, 12);
  169.    printf("\n wrote %d bytes", bytes_written);
  170.  
  171.       /* Bits Per Pixel */
  172.    insert_short_into_buffer(buffer, 0, 258);
  173.    insert_short_into_buffer(buffer, 2, 3);
  174.    insert_short_into_buffer(buffer, 4, 1);
  175.    insert_short_into_buffer(buffer, 8, image_header->bits_per_pixel);
  176.  
  177.    bytes_written = my_write(file_desc, buffer, 12);
  178.    printf("\n wrote %d bytes", bytes_written);
  179.  
  180.       /* Strip Offset */
  181.    insert_short_into_buffer(buffer, 0, 273);
  182.    insert_short_into_buffer(buffer, 2, 3);
  183.    insert_short_into_buffer(buffer, 4, 1);
  184.    insert_short_into_buffer(buffer, 8, 74);
  185.    bytes_written = my_write(file_desc, buffer, 12);
  186.    printf("\n wrote %d bytes", bytes_written);
  187.  
  188.  
  189.       /* Offset to next IFD (0 means no more IFD's) */
  190.    buffer[0]  = 0x00;
  191.    buffer[1]  = 0x00;
  192.    buffer[2]  = 0x00;
  193.    buffer[3]  = 0x00;
  194.  
  195.    bytes_written = my_write(file_desc, buffer, 4);
  196.    printf("\n wrote %d bytes", bytes_written);
  197.    printf("\n length is %ld", image_header->image_length);
  198.    printf("\n width is %ld", image_header->image_width);
  199.  
  200.  
  201.    round_off_image_size(image_header, &l, &w);
  202.    k = l * w;
  203.  
  204.    if(image_header->bits_per_pixel == 8)
  205.       k = k/2;
  206.    else
  207.       k = k/4;
  208.    k++;
  209.  
  210.    for(i=0; i<ROWS; i++)
  211.       for(j=0; j<COLS; j++)
  212.         image[i][j] = 0;
  213.  
  214.    j = sizeof(short) * ROWS * COLS;
  215.  
  216.    for(i=0; i<k; i++){
  217.       bytes_written = my_write(file_desc, image, j);
  218.       printf("\n wrote %d bytes", bytes_written);
  219.    }
  220.  
  221.  
  222.    close(file_desc);
  223.  
  224. }  /* ends create_allocate_tiff_file */
  225.  
  226.  
  227.  
  228.  
  229.  
  230.        /*******************************************************
  231.        *
  232.        *   write_array_into_tiff_file(...
  233.        *
  234.        *   This function takes an array of shorts and writes
  235.        *   them into an existing tiff image file.
  236.        *
  237.        *******************************************************/
  238.  
  239.  
  240.  
  241. write_array_into_tiff_image(image_file_name, array,
  242.                             il, ie, ll, le)
  243.  
  244.         char    image_file_name[];
  245.         int     il, ie, ll, le;
  246.         short   array[ROWS][COLS];
  247. {
  248.  
  249.    char  buffer[COLS];
  250.  
  251.    int   bytes_written,
  252.          closed,
  253.          file_descriptor,
  254.          i,
  255.          written;
  256.  
  257.    float a;
  258.  
  259.    long  line_length,
  260.          offset,
  261.          position;
  262.  
  263.    struct tiff_header_struct image_header;
  264.  
  265.  
  266.  
  267.    read_tiff_header(image_file_name, &image_header);
  268.  
  269.  
  270.       /****************************************************
  271.       *
  272.       *   Procedure:
  273.       *   Seek to the strip offset where the data begins.
  274.       *   Seek to the first line you want.
  275.       *   Loop over the lines you want to write.
  276.       *      Seek to the first element of the line.
  277.       *      Write the line.
  278.       *      Seek to the end of the data in that line.
  279.       *
  280.       ****************************************************/
  281.  
  282.    file_descriptor = my_open(image_file_name);
  283.    position        = lseek(file_descriptor,
  284.                      image_header.strip_offset, 0);
  285.    position        = seek_to_first_line(file_descriptor,
  286.                                         &image_header, il);
  287.  
  288.    for(i=0; i<(ll-il); i++){
  289.       offset        = (ie-1)/(8/image_header.bits_per_pixel);
  290.       position      = lseek(file_descriptor, offset, 1);
  291.       bytes_written = write_line(file_descriptor, array,
  292.                                  i, &image_header, ie, le);
  293.       position      = seek_to_end_of_line(file_descriptor,
  294.                                           le, &image_header);